// 1 html代码用js动态加载
//   <script type="text/html" id="T-pcList">   </   T-pcList对应html标签 
// 
$(function(){
	var s = $('#T-pcList').html()  // 获得js的html内容
	$(".picScroll-left .bd").html(s)  // 将s的内容放到class为bd内
	thisstyle()  // 执行某个函数
})

//2 获取当前日期
var calculateDate = function(){
	var date = new Date()
	var weeks = ["日","一","二","三","四","五","六"]
	return date.getFullYear()+"年"+(date.getMonth()+1)+"月"+
		date.getDate()+"日 星期"+weeks[date.getDay()]
}

$(function(){
	$("#dateSpan").html(calculateDate())
})

// 3 时间倒计时(固定倒计时的技术时间)
function countdown(){
	var endtime = new Date("")  // 结束时间
	var nowtime = new Date()
	if(nowtime >= endtime){
		document.getElementById('_lefttime').innerHTML = "倒计时结束"
		return
	}
	var leftsecond = parseInt((endtime.getTime()-nowtime.getTime())/1000)
	if(leftsecond < 0){
		leftsecond = 0
	}
	_d = parseInt(leftsecond / 3600 / 24)
	_h = parseInt((leftsecond / 3600) %24)
	_m = parseInt((leftsecond / 60)%60)
	_s = parseInt(leftsecond % 60)
	document.getElementById('_lefttime').innerHTML = _d + '天' + _h +"小时"+
	_m + "分"+_s +"秒"
}
countdown()
setInterval(countdown,1000)

// 4 10秒倒计时跳转
// <div id="showtimes"></div>

function showTime(){
	t -=1
	document.getElementById('showtimes').innerHTML = t
	if(t==0){
		location.href = "error404.asp"  //跳转位置
	}
	setTimeout(showTime,1000)
}

showTime()

// 5 点击某个div区域之外，隐藏该div
// 一般写法
$(document).on('click',function(e){
	var target = $(e.target)
	if(target.closest(".city_box,#city_select a.selected").length == 0){
		$(".city_box").hide()
	}
})
// 全面
$(document).on('click',function(e){
	var _con = $('目标区域')  // 设置目标区域
	if(!_con.is(e.target) && _con.has(e.target).length === 0){ // mark1
		// some code... 功能代码
	}
})
// mark1原理： 判断点击事件发生在区域外的条件是1、点击事件的对象不是目标区域本身
// 2事件对象同时也不是目标区域的子元素

// 6  获取某年某月的哪些天是周六和周日

function time(y,m){
	var tempTime = new Date(y,m,0)
	var time = new Date()
	var saturday = new Array()
	var sunday = new Array()
	for(var i=1;i<tempTime.getDate();i++){
		time.setFullYear(y,m-1,i)
		var day = time.getDay()
		if(day == 6){
			saturday.push(i)
		}else if(day == 0){
			sunday.push(i)
		}
	}
	var text = y +"年"+m+"月份"+"<br/>"+
	 "周六："+saturday.toString() + "<br/>"+
	 "周日："+sunday.toString()
	 document.getElementById("text").innerHTML = text
}
time(2014,7)


// 7   JS加入收藏代码
function addFavorte(title,url){
	url = encodeURI(url)
	try{
		window.external.addFavorte(url,title)
	}catch(e){
		try{
			window.sidebar.addPanel(title,url,"")
		}catch(e){
			alert("加入收藏失败，ctrl+d进行添加")
		}
	}
}
addFavorte(document.title,window.location)

// 8  js强制手机页面横屏显示
$(window).on("orientationchange",function(event){
	if(event.orientation == 'portrait'){
		$('body').css('transform','rotate(90deg)')
	}else{
		$('body').css('transform','rotate(0deg)')
	}
})
$(window).orientationchange()

// 9  获取上传文件的大小
// <input type="file" id="filepath" cochange="getFileSize(this)" />
function getFileSize(obj){
	var filesize;
	if(obj.files){
		filesize = obj.files[0].size
	}else{
		try{
			var path,fso;
			path = document.getElementById("filepath").value
			fso = new ActiveXObject("Scripting.FileSystemObject")
			filesize = fso.GetFile(path).size
		}catch(e){
			// 在ie9及低版本浏览器，如果不容许activeX控件与页面交互，点击了否，就无法获取size
			console.log(e.message)
			filesize = "error"
		}
	}
	return filesize
}
// 10 跨浏览器获取可视窗口大小
function getWindow(){
	if(typeof window.innerWidth !='undefined'){
		return{
			width:window.innerWidth,
			height:window.innerHeight
		}
	}else{
		return{
			width:document.documentElement.innerWidth,
			height:document.documentElement.innerHeight
		}
	}
}

// 11  js获取屏幕坐标
// x:<input id="xxx" type="text"> Y:<input id="yyy" type="text">
function mousePosition(ev){
	if(ev.pageX || ev.pageY){
		return{x:ev.pageX,y:ev.pageY}
	}
	return{
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop - document.body.clientTop,
	}
}

function mouseMove(ev){
	ev = ev || window.event
	var mousePos = mousePosition(ev)
	document.getElementById('xxx').value = mousePos.x
	document.getElementById('yyy').value = mousePos.y
}



// 12   js拖拽效果
var oDiv = document.getElementById("login")
oDiv.onmousedown = function(e){
	var e= e|| window.event
	var diffX = e.client - oDiv.offsetLeft
	var diffY = e.clientY - oDiv.offsetTop
	document.onmousemove = function(e){
		var e = e|| window.event
		oDiv.style.left = e.clientX  -diffX + 'px'
		oDiv.style.top = e.clientY  -diffY + 'px'
	}
	document.onmouseup = function(){
		document.onmousemove = null
		document.onmouseup = null
	}
}

// 13 js动态创建css样式添加到head内
function addCSS(cssText){
	var style = document.createElement('style')
	var head = document.head || document.getElementByTagName('head')[0]
	style.type = 'text/css'
	if(style.styleSheet){
		var func = function(){
			try{
				style.styleSheet.cssText = cssText;
			}catch(e){
				
			}
		}
		if(style.styleSheet.disabled){
			setTimeout(func,10)
		}else{
			func()
		}
	}else{
		var textNode = document.createTextNode(cssText)
		style.appendChild(textNode)
	}
	head.appendChild(style)
}
addCSS('#demo{height:30px;background:#f00}')

// 14随机产生lowwer-upper之间的随机数
function selectFrom(lower,upper){
	var sum = upper - lower +1
	return Math.floor(Math.random()*sum+lower)
}

// 15 保留后端传递到前端页面的空格
var objt = {
	name:'aaa'
}
objt.name = objt.name.replace(/\s/g,'&nbsp;')
console.log(objt.name)

// 16 每隔0.1s改变图片的路径
// <div id='tt'><img src="" />
(function(){
	function changeimagesrc(t){
		document.getElementById('tt').childNodes[0].src='images/'+t+".jpg"
	}
	setInterval(function(){
		for(var i=0;i<2;i++){
			setTimeout((function(t){
				return function(){
					changeimagesrc()
				}
			})(i+1),i*100)
		}
	},1000)
})()

// 17 实现密码框输入聚焦、失焦问题
// <input id="i_input" type="text" value="">
window.onload = function(){
	var oIpt = document.getElementById("i_input")
	if(oIpt.value == ''){
		oIpt.style.color = '#888'
	}else{
		oIpt.style.color = '#000'
	}
	oIpt.onfocus = function(){
		if(this.value == ''){
			this.value = ''
			this.style.color = "#000"
			this.type="password"
		}else{
			this.style.color = '#000'
		}
	}
	oIpt.onblur = function(){
		if(this.value == ''){
			this.value = ''
			this.style.color ='#888'
			this.type = "text"
		}
	}
}

// 18 js 对象冒充
function Person(name,age){
	this.name = name
	this.age = age
	this.say = function(){
		return "name:"+this.name+"age:"+this.age
	}
}

var o = new Object()
Person.call(o,'zhangsan',20)
console.log(o.say()) // name: zhangsan age:20